package com.cmw.auntapp.component;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cmw.auntapp.R;
import com.cmw.auntapp.ui.PicChooseActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;

/**
 * @desc: 文件/图片选择/拍照弹窗
 * @author: chengmingwei
 * @date: 2021/5/13 9:00 PM
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */
public class FileChoosePopWindow extends PopupWindow {

    private Context mContext;

    private int maxSelectNum = 9;

    private int selectionMode = PictureConfig.SINGLE;

    private int sourceViewId;
    
    private View selectView;

    public FileChoosePopWindow(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public View getSelectView() {
        return selectView;
    }

    public void setSelectView(View selectView) {
        this.selectView = selectView;
    }

    /**
     * 获取触发文件选择的事件源ViewId
     * @return  返回触发文件选择的事件源View
     */
    public int getSourceViewId() {
        return sourceViewId;
    }

    /**
     * 触发文件选择的事件源ViewId
     * @param sourceViewId    设置触发文件选择的事件源ViewId
     */
    public void setSourceViewId(int sourceViewId) {
        this.sourceViewId = sourceViewId;
    }

    private void init() {
        View bottomView = View.inflate(mContext, R.layout.layout_bottom_dialog, null);
        setContentView(bottomView);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);


        TextView mAlbum = (TextView) bottomView.findViewById(R.id.tv_album);
        TextView mCamera = (TextView) bottomView.findViewById(R.id.tv_camera);
        TextView mCancel = (TextView) bottomView.findViewById(R.id.tv_cancel);

        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        Activity rootView = (Activity)mContext;
        setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = rootView.getWindow().getAttributes();
                lp.alpha = 1f;
                rootView.getWindow().setAttributes(lp);
            }
        });
        setAnimationStyle(R.style.main_menu_photo_anim);
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tv_album:
                        //相册
                        PictureSelector.create(rootView)
                                .openGallery(PictureMimeType.ofImage())
                                .maxSelectNum(maxSelectNum)
                                .minSelectNum(1)
                                .imageSpanCount(4)
                                .selectionMode(selectionMode) // PictureConfig.MULTIPLE
                                .forResult(PictureConfig.CHOOSE_REQUEST);

                        break;
                    case R.id.tv_camera:
                        //拍照
                        PictureSelector.create(rootView)
                                .openCamera(PictureMimeType.ofImage())
                                .forResult(PictureConfig.CHOOSE_REQUEST);
                        break;
                    case R.id.tv_cancel:
                        //取消
                        //closePopupWindow();
                        break;
                }
                closePopup();
            }
        };

        mAlbum.setOnClickListener(clickListener);
        mCamera.setOnClickListener(clickListener);
        mCancel.setOnClickListener(clickListener);
    }

    /**
     * 当打开相册选相片时只能一次选择一张
     */
    public void showPopup() {
        selectionMode = PictureConfig.SINGLE;
        Activity rootView = (Activity)mContext;
        WindowManager.LayoutParams lp = rootView.getWindow().getAttributes();
        lp.alpha = 0.5f;
        rootView.getWindow().setAttributes(lp);
        showAtLocation(rootView.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    /**
     * 打开相册（可多选图片）
     */
    public void showPopupMulti() {
        selectionMode = PictureConfig.MULTIPLE;
        showPopup();
    }

    public void closePopup() {
        if (isShowing()) {
            dismiss();
        }
    }
}
